﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using Csla.Rules;
using Csla.Core;
using System.Text.RegularExpressions;

namespace CslaHispano.EncapsulatedInvoke.Domain.Core
{
    public class CustomIsNumericRule : Csla.Rules.BusinessRule
    {
        public CustomIsNumericRule(IPropertyInfo primaryProperty)
            : base(primaryProperty)
        {
            InputProperties = new List<IPropertyInfo> { PrimaryProperty };
        }

        protected override void Execute(RuleContext context)
        {
            var target = (string)context.InputPropertyValues[PrimaryProperty];
            if (IsNumeric(target))
                context.AddErrorResult("The code must be alphanumeric.");
        }

        private bool IsNumeric(string text)
        {
            Regex _pattern = new Regex("[^0-9]");
            return !_pattern.IsMatch(text)
                 && (text != "");
        }
    }
}
